import { Inject, Provide } from '@midwayjs/core'
import { Context } from '@midwayjs/koa'
import UAParser from 'ua-parser-js'

@Provide()
export class UaService {
  @Inject()
  ctx: Context

  /** 浏览器集合 */
  BROWSER = [
    [/(apifox)\/([\w\.]+)/i],
    [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]
  ]

  getByCtx() {
    const userAgent = this.ctx.req.headers['user-agent']
    const uaParser = new UAParser(userAgent, {
      browser: this.BROWSER
    })

    return uaParser.getResult()
  }
}
